﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>分类管理</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/Static/jquery-1.6.2.min.js" type="text/javascript"></script>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <script src="/Static/boot.js" type="text/javascript"></script>
    <link href="/Static/demo.css" rel="stylesheet" />
    <link href="/Static/miniui/themes/blue/skin.css" rel="stylesheet" />

    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <div class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="270" showcollapsebutton="false">
            <!--<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <span style="padding-left: 5px;">名称：</span>
                <input class="mini-textbox" id="txt_key" />
                <a class="mini-button" iconcls="icon-search" plain="true" onclick="SearchCateyoryName">查找</a>
            </div>-->
            <div class="mini-fit">
                <ul id="tr_CategoryTree" class="mini-tree" url="/Ajax/AjaxService.aspx?method=GetCategory&pageSize=100000"
                    style="width: 100%;" contextmenu="#treeMenu" showtreeicon="true" expandonload="true"
                    textfield="CategoryDisplayName" idfield="ID" parentfield="ParentCategoryID" resultastree="false"
                    expandonnodeclick="true" onnodeselect="nodeselect"></ul>
                <ul id="treeMenu" class="mini-contextmenu" onbeforeopen="onBeforeOpen">
                    <li name="li_AddChildNode" iconcls="icon-add" onclick="AddRootNode">添加同级节点</li>
                    <li name="li_AddChildNode" iconcls="icon-edit" onclick="AddChildNode">添加子节点</li>
                    <!--<li name="li_RemoveNode" iconcls="icon-remove" onclick="RemoveNode">删除节点</li>-->

                </ul>
            </div>
        </div>
        <div showcollapsebutton="false">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <input id="hn_action" class="mini-textbox" allowinput="false" value="insertRoot" style="width: 100px;
                    display: none" />
                <input id="txt_actionName" class="mini-textbox" allowinput="false" value="添加分类信息"
                       style="width: 100px; font-weight:bold;" />
            </div>
            <fieldset style="border: solid 1px #aaa; padding: 3px;">
                <legend><b>分类基本信息</b></legend>
                <div id="form" style="padding-left: 11px; padding-bottom: 5px;">
                    <div style="padding-left: 11px; padding-bottom: 5px;">
                        <table style="table-layout: fixed;">
                            <tr>
                                <td style="width: 100px; text-align: right">
                                    ID：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_ID" name="ID" class="mini-textbox" allowinput="false" style="width: 100px;" />
                                </td>
                            </tr>

                            <tr>
                                <td style="width: 100px; text-align: right">
                                    上级：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_ParentCategoryID" class="mini-buttonedit" onbuttonclick="onSelectCategory" allowinput="false"
                                           required="true" style="width: 200px;" />
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; text-align: right">
                                    分类名称：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_CategoryName" name="CategoryName" class="mini-textbox" style="width: 200px; " required="true" />
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; text-align: right">
                                    分类显示名称：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_CategoryDisplayName" name="CategoryDisplayName" class="mini-textbox" style="width: 200px;" required="true" />
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; text-align: right">
                                    分类Code：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_CategoryCode" name="CategoryCode" class="mini-textbox" style="width: 200px;" required="true" />
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; text-align: right">
                                    启用状态：
                                </td>
                                <td style="width: 230px;">
                                    <input id="txt_IsAvailable" class="mini-combobox" valuefield="id" textfield="text" valuefromselect="true"
                                           shownullitem="false" url="/ConfigData/IsActive.txt" width="200px" required="true" />
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px;"></td>
                                <td style="width: 230px; text-align: left">
                                    <a class="mini-button" style="width: 200px; height: 25px; font-size: 16px; line-height: 25px"
                                       plain="false" onclick="saveCategory()">保&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;存</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </fieldset>

        </div>
    </div>

    <script type="text/javascript">
        mini.parse();
        var tree = mini.get("tr_CategoryTree");

        mini.get("txt_ID").setEnabled(false);
        mini.get("txt_IsAvailable").setValue("Y");

        // grid.load();
        /**点击架构树加载信息*********************************************************************/

        function nodeselect() {
            var selectNode = tree.getSelectedNode();
            var categoryID = selectNode.ID;
            
            $.ajax({
                url: "/Ajax/AjaxService.aspx?method=GetCategory",
                data: { action: "Edit", CategoryID: categoryID },
                cache: false,
                success: function(text) {
                    var result = jQuery.parseJSON(text);
                    //设置分类信息
                    setCategory(result, selectNode);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });

            //加载流程的所有信息
            //grid.load({ CategoryID: categoryID });
        }

/**设置信息*********************************************************************/
        function setCategory(result, selectNode) {
            initCategory("edit", "编辑分类信息");
            
            mini.get("txt_ID").setValue(result[0].ID);
            mini.get("txt_CategoryName").setValue(result[0].CategoryName);
            mini.get("txt_CategoryDisplayName").setValue(result[0].CategoryDisplayName);
            mini.get("txt_CategoryCode").setValue(result[0].CategoryCode);

            if (result[0].ParentCategoryID <= 0 || result[0].ParentCategoryID == "0") {
                mini.get("txt_ParentCategoryID").setValue("0");
                mini.get("txt_ParentCategoryID").setText("");
            }
            else {
                mini.get("txt_ParentCategoryID").setValue(result[0].ParentCategoryID);
                var parentNode = tree.getParentNode(selectNode);
                mini.get("txt_ParentCategoryID").setText(parentNode.CategoryDisplayName);
            }

            mini.get("txt_IsAvailable").setValue(result[0].IsAvailable ? "Y" : "N");

        }
        /**初始化信息*********************************************************************/
        function initCategory(action, actionName) {
            mini.get("hn_action").setValue(action);
            mini.get("txt_actionName").setValue(actionName);
            //清空信息
            mini.get("txt_ID").setValue("");
            mini.get("txt_CategoryName").setValue("");
            mini.get("txt_CategoryDisplayName").setValue("");
            mini.get("txt_ParentCategoryID").setValue("");
            mini.get("txt_ParentCategoryID").setText("");
            mini.get("txt_IsAvailable").setValue("Y");

            var node = tree.getSelectedNode();
            //根据操作类型赋值
            switch (action) {
                case "insertRoot"://新添加根节点
                    mini.get("txt_IsAvailable").setValue("Y");
                    var par = tree.getParentNode(node);
                    //设置上级父节点
                    mini.get("txt_ParentCategoryID").setValue(tree.getParentNode(node).ID);
                    mini.get("txt_ParentCategoryID").setText(tree.getParentNode(node).CategoryDisplayName);
                    break;
                case "insertChild"://添加子节点

                    mini.get("txt_IsAvailable").setValue("Y");
                    mini.get("txt_ParentCategoryID").setValue(node.ID);
                    mini.get("txt_ParentCategoryID").setText(node.CategoryDisplayName);
                    break;
                default://其他，编辑信息
                    break;
            };
        }

        /*添加根节点*******************************************************************/
        function AddRootNode() {
            initCategory("insertRoot", "添加根节点");
        }
        /**添加子节点*****************************************************************/
        function AddChildNode() {
            initCategory("insertChild", "添加子节点");
        }
        /**删除节点******************************************************************/
        function RemoveNode() {
            // var ids = GetCheckNodeIDs();
            //  alert(ids);
            mini.showMessageBox({
                title: "删除警告",
                iconCls: "mini-messagebox-question",
                buttons: ["ok", "cancel"],
                message: "将删除所选节点值，此操作不可恢复，请慎重！",
                callback: function (action) {
                    if (action == "cancel") return;
                    if (action == "ok") {
                        $.ajax({
                            url: "/Ajax/AjaxService.aspx?method=EditCategory",
                            data: {
                                CategoryIDs: GetCheckNodeIDs(),
                                action: "Delete"
                            },
                            cache: false,
                            success: function (text) {
                                var o = mini.decode(text);
                                //设置部门信息
                                mini.alert(o.res);
                                //重新加载架构树

                                tree.load("/Ajax/AjaxService.aspx?method=GetCategory");
                                // tree.selectNode(0);
                                //  tree.expandPath(o.data);
                                // tree.scrollIntoView(o.data);
                                // nodeselect();
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert("ajax删除错误");
                            }
                        });

                    }
                }
            });

        }

        //获取选中节点及其所有子节点
        function GetCheckNodeIDs() {
            //选中节点
            var selfNode = tree.getSelectedNode();
            var nodeids = selfNode.ID;
            //选中节点的所有子节点
            var childNodes = tree.getAllChildNodes(selfNode.ID);
            for (var i = 0; i < childNodes.length; i++) {
                nodeids = nodeids + "," + childNodes[i].ID;
            }

            return nodeids + ",";

        }
        /**保存信息**************************************************************/
        function saveCategory() {
            var form = new mini.Form("#form");
            var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;

            if (parseInt(mini.get("txt_ID").getValue()) == parseInt(mini.get("txt_ParentCategoryID").getValue())) {
                mini.alert("不允许将自己添加到自己分类下面!");
                return false;
            }
            $.ajax({
                url: "/Ajax/AjaxService.aspx?method=EditCategory",
                data: {
                    Action: mini.get("hn_action").getValue(),
                    ID: mini.get("txt_ID").getValue(),
                    CategoryName: mini.get("txt_CategoryName").getValue(),
                    CategoryDisplayName: mini.get("txt_CategoryDisplayName").getValue(),
                    CategoryCode: mini.get("txt_CategoryCode").getValue(),
                    ParentCategoryID: mini.get("txt_ParentCategoryID").getValue(),
                    IsAvailable: mini.get("txt_IsAvailable").getValue()
                },
                cache: false,
                success: function (text) {
                    var o = mini.decode(text);
                    //重新加载架构树
                    if (o.outID != "") {
                        // alert("操作成功【ID：" + o.outID + "】！");
                        alert("操作成功!");
                        tree.load("/Ajax/AjaxService.aspx?method=GetCategory");
                        tree.selectNode(o.outID);
                        tree.expandPath(o.outID);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("ajax错误");
                }
            });

        }
        /**加载***********************************************************/
        function onSelectCategory(e) {
            var btnEdit = this;
            var parentID = btnEdit.getValue();
            mini.open({
                url: "/Parts/pub/SelectCategory.html",
                showMaxButton: false,
                title: "选择分类",
                width: 350,
                height: 450,
                onload: function () {
                    var iframe = this.getIFrameEl();

                    if (parentID != "") {
                        iframe.contentWindow.SetDefaultSelect({ ParentID: parentID });
                    }
                },
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        if (data) {
                            btnEdit.setValue(data.ID);
                            btnEdit.setText(data.CategoryDisplayName);
                        }
                    }
                    if (action == "clear") {
                        btnEdit.setValue("");
                        btnEdit.setText("");
                    }

                }
            });
        }
        function SearchCateyoryName() {

            //var categoryName = mini.get("txt_key").getValue();
            //var url = "../Ajax/AjaxService.aspx?method=GetCategory&CategoryName=" + categoryName;
            //url = encodeURI(url); //两次编码
            //url = encodeURI(url);
            //tree.setUrl(url);

            //tree.load("../Ajax/AjaxService.aspx?method=GetCategory&CategoryName=" + categoryName);
        }
        /**树节点右键菜单*********************************************************************/
        function onBeforeOpen(e) {
            ;
            var menu = e.sender;
            var node = tree.getSelectedNode();
            if (!node) {
                e.cancel = true;
                return;
            }
        }
    </script>
</body>
</html>
